package p1;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;;

public class Semester {
	private List<Class> _classes;
	
	/*
	 * Note that year and season are strings.
	 * Why?	Because strings are very friendly in java. Portability, ease of use,
	 * 		and simple conversions. Also there is no need for scientific numerical
	 * 		accuracy so we don't need to use an entire number to represent what's
	 * 		essentially a tiny range of possible integer and floating point values.
	 */
	private String _year;
	private String _season;
	public static String Default_Year_Season = "";

	public Semester()
	{
		_classes = new ArrayList<Class>();
		_year = Semester.Default_Year_Season;
		_season = Semester.Default_Year_Season;
	}
	public Semester(String year, String season)
	{
		_classes = new ArrayList<Class>();
		this.setYear(year);
		this.setSeason(season);
	}
	
	public List<Class> getClasses() 
	{ return ((List<Class>) ((ArrayList<Class>) _classes).clone()); }
	public void setClasses(List<Class> classes)
	{ _classes = (List<Class>)((ArrayList<Class>)classes).clone(); }
	
	public String getYear(){ return _year; }
	public void setYear(String s){
		int year = Integer.parseInt(s);
		
		if(year < 2011 || year > 2050) return;
		
		_year = String.valueOf(year);
	}
	
	public String getSeason(){ return _season; }
	public void setSeason(String s){
		s = s.toLowerCase();
		
		boolean valid = false;
		if(s.equals("fall"))
			valid = true;
		if(s.equals("winter"))
			valid = true;
		if(s.equals("summer"))
			valid = true;
		if(s.equals("spring"))
			valid = true;
		
		if(valid) _season = s;
	}
	
	public void addClass(Class c)
	{ _classes.add(c); }
	public boolean removeClass(Class c)
	{ 
		Iterator<Class> it = _classes.iterator();
		while(it.hasNext()){
			Class temp = (Class)it.next();
			if(temp.getCredits() == c.getCredits() 
				&& temp.getTaken() == c.getTaken())
				{
					_classes.remove(temp);
					return true;
				}
		}
		return false;
	}
	
	public String toString(){
		Iterator<Class> it = _classes.iterator();
		int c = 1;
		String output = "";
		while(it.hasNext()){
			Class temp = (Class)it.next();
			output += "Semester:\n\t<Year>:\t\t"+_year+"\n\t<Season>:\t"+_season+"\n";
			output += "\t<Class>["+c+"]:\n\tCredits:\t"+temp.getCredits()+"\n\tTaken:\t\t"+temp.getTaken()+"\n";
			c++;
		}
		return output;
	}
}
